﻿using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using static xsb.core.Data;

namespace xsb.core
{
    class XlsUtil
    {
        public static bool decodeFromXls(string path)
        {
            all_person = new List<Person>();

            FileStream fileStream;
            IWorkbook workbook;
            ISheet sheet;

            try
            {
                using (fileStream = File.OpenRead(path))
                {
                    if (path.EndsWith(".xlsx"))
                    {
                        workbook = new XSSFWorkbook(fileStream);
                    }
                    else { return true; }


                    if (workbook == null)
                        return true;

                    sheet = workbook.GetSheetAt(0);


                    if (sheet == null)
                        return true;


                    if (sheet.LastRowNum <= 0)
                        return true;


                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null)
                            continue;

                        Person person = new Person();

                        int index = 0;
                        {
                            ICell cell = row.GetCell(index++);
                            if(cell != null)
                                person.id = cell.ToString();
                        }
                        {
                            ICell cell = row.GetCell(index++);
                            if (cell != null)
                                person.name = cell.ToString();
                        }
                        {
                            ICell cell = row.GetCell(index++);
                            if (cell != null)
                                person.is_male = cell.ToString()=="男";
                        }
                        {
                            ICell cell = row.GetCell(index++);
                            if (cell != null)
                                person.college = cell.ToString();
                        }
                        {
                            ICell cell = row.GetCell(index++);
                            if (cell != null)
                                person.seed = cell.ToString()=="是";
                        }

                        all_person.Add(person);
                    }

                }
            } 
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
                return true;
            }

            return false;
        }

        static readonly string[] columns = { "组别", "学号", "姓名", "性别", "学院", "种子选手" };
        public static void encodeToXls(List<List<Group>> sheets, string path)
        {
            IWorkbook workbook = new XSSFWorkbook();

            for(int k=0; k<sheets.Count; k++)
            {
                ISheet sheet = workbook.CreateSheet("编排"+(k+1));
                List<Group> groups = sheets[k];

                //列名
                {
                    IRow row = sheet.CreateRow(0);
                    for (int i = 0; i < columns.Length; i++)
                    {
                        ICell cell = row.CreateCell(i);
                        cell.SetCellValue(columns[i]);
                    }
                }

                //实际内容
                int rindex = 1;
                for (int i = 0; i < groups.Count; i++)
                {
                    for (int j = 0; j < groups[i].members.Count; j++)
                    {
                        IRow row = sheet.CreateRow(rindex++);
                        int cindex = 0;
                        {
                            ICell cell = row.CreateCell(cindex++);
                            cell.SetCellValue((i+1).ToString());
                        }
                        {
                            ICell cell = row.CreateCell(cindex++);
                            cell.SetCellValue(groups[i].members[j].id);
                        }
                        {
                            ICell cell = row.CreateCell(cindex++);
                            cell.SetCellValue(groups[i].members[j].name);
                        }
                        {
                            ICell cell = row.CreateCell(cindex++);
                            cell.SetCellValue(groups[i].members[j].is_male?"男":"女");
                        }
                        {
                            ICell cell = row.CreateCell(cindex++);
                            cell.SetCellValue(groups[i].members[j].college);
                        }
                        {
                            ICell cell = row.CreateCell(cindex++);
                            cell.SetCellValue(groups[i].members[j].seed ? "是" : "否");
                        }
                    }
                }
            }

            try
            {
                using (FileStream fileStream = File.Create(path))
                {
                    workbook.Write(fileStream);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("文件输出失败");
                return;
            }

            MessageBox.Show("编排完毕。文件输出到了 "+path);
        }
    }
}
